import { Amount, Currency, IssuedCurrency, XChainBridge } from "xrpl/dist/npm/models/common";
import { xrpToDrops } from "./utils";

export type BridgeChain = {
    type: "xrp" | "evm";
    nodeUrl: string;
    bridgeAccount: string;
    feePrivateKey: string;
    currency: Currency;
    XChainBridge: XChainBridge;
    tokenAddress: string;
};

export type BridgeParams = {
    threshold: number;
    signatureReward: number;
    minCreateAmount: number;
};

export type BridgeConfigType = {
    lockingChain: BridgeChain;
    issuingChain: BridgeChain;
    params: BridgeParams;
};

export class BridgeConfig {
    constructor(public readonly config: BridgeConfigType) {}

    get isTokenBridge() {
        return this.config.lockingChain.currency.currency !== "XRP";
    }

    bridgeChain(chain: "xrp" | "evm") {
        return this.config.lockingChain.type === chain ? this.config.lockingChain : this.config.issuingChain;
    }

    parseXrpAmount(amount: number): Amount {
        return !this.isTokenBridge
            ? xrpToDrops(amount)
            : {
                  ...(this.bridgeChain("xrp").currency as IssuedCurrency),
                  value: amount.toString(),
              };
    }
}
